﻿namespace KeePassLib
{
    using System;
    using System.Runtime.InteropServices.WindowsRuntime;

    using Windows.Security.Cryptography.DataProtection;
    using Windows.Storage.Streams;

    public static class DataProtector
    {
        private const string ProtectionDescriptor = "LOCAL=user";

        public static byte[] Protect(byte[] data)
        {
            var dp = new DataProtectionProvider(ProtectionDescriptor);

            IBuffer buffer = dp.ProtectAsync(data.AsBuffer()).AsTask().Result;
            byte[] protectedData = buffer.ToArray();
            return protectedData;
        }

        public static byte[] Unprotect(byte[] protectedData)
        {
            var dp = new DataProtectionProvider(ProtectionDescriptor);
            byte[] data = dp.UnprotectAsync(protectedData.AsBuffer()).AsTask().Result.ToArray();
            return data;
        }
    }
}
